package com.hotel.quertz;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * Quartz配置类
 */
@Configuration
public class QuartzConfig {
    /**
     * 1、创建Job对象
     */
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean(){
        JobDetailFactoryBean factoryBean=new JobDetailFactoryBean();
        //关联我们自己的Job类
        factoryBean.setJobClass(QuartzDemo.class);
        return factoryBean;
    }

    /**
     * cron触发
     *
     * @param jobDetailFactoryBean 工作细节工厂bean
     * @return {@link CronTriggerFactoryBean}
     */
    @Bean
    CronTriggerFactoryBean cronTrigger(JobDetailFactoryBean jobDetailFactoryBean){
        CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
        bean.setJobDetail(jobDetailFactoryBean().getObject());
        bean.setStartDelay(2000);
        bean.setCronExpression("0 */2 * * * ?");
        return bean;
    }

    /**
     * 3、创建Scheduler
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cro,MyadaptableJobFactory myadaptableJobFactory){
        SchedulerFactoryBean factoryBean=new SchedulerFactoryBean();
        //关联trigger
        factoryBean.setTriggers(cro.getObject());
        factoryBean.setJobFactory(myadaptableJobFactory);
        return factoryBean;
    }

}